<template>
  <div class="app-container">
    <el-card class="box-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <div class="header-left">
            <el-icon class="header-icon"><Goods /></el-icon>
            <span class="header-title">商品品牌管理</span>
          </div>
          <el-button type="primary" :icon="Plus" @click="handleAdd">新增品牌</el-button>
        </div>
      </template>

      <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true" class="search-form">
        <el-form-item label="品牌名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入品牌名称" clearable style="width: 200px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 200px">
            <el-option v-for="dict in statusOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="brandList" border stripe highlight-current-row>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="品牌ID" align="center" prop="id" width="80" />
        <el-table-column label="品牌名称" align="center" prop="name" min-width="120" />
        <el-table-column label="品牌Logo" align="center" min-width="120">
          <template #default="scope">
            <el-image 
              v-if="scope.row.logo" 
              :src="scope.row.logo" 
              fit="contain" 
              class="brand-logo"
              :preview-src-list="[scope.row.logo]"
              :preview-teleported="true"
            >
              <template #error>
                <div class="image-slot">
                  <el-icon><Picture /></el-icon>
                  <span>暂无图片</span>
                </div>
              </template>
            </el-image>
            <div v-else class="image-slot">
              <el-icon><Picture /></el-icon>
              <span>暂无图片</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="品牌描述" align="center" prop="description" min-width="200" show-overflow-tooltip />
        <el-table-column label="排序" align="center" prop="sort" width="80" />
        <el-table-column label="状态" align="center" prop="status" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'" effect="light">
              {{ scope.row.statusName }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="160" show-overflow-tooltip />
        <el-table-column label="操作" align="center" width="150" fixed="right">
          <template #default="scope">
            <el-button type="primary" link icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button type="danger" link icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        :total="total"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改品牌对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body destroy-on-close>
      <el-form ref="brandRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="品牌名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入品牌名称" />
        </el-form-item>
        <el-form-item label="品牌Logo" prop="logo">
          <image-upload v-model="form.logo" :is-show-tip="false" />
        </el-form-item>
        <el-form-item label="品牌描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入品牌描述" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="0" :max="999" controls-position="right" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :value="1">
              <el-icon><CircleCheck /></el-icon>
              正常
            </el-radio>
            <el-radio :value="0">
              <el-icon><CircleClose /></el-icon>
              禁用
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  Plus, 
  Edit, 
  Delete, 
  Goods, 
  Picture,
  CircleCheck,
  CircleClose
} from '@element-plus/icons-vue'
import { listBrand, getBrand, addBrand, updateBrand, delBrand } from '@/api/product/brand'
import type { ProductBrandBo, ProductBrandVo } from '@/api/product/brand/types'
import ImageUpload from '@/components/ImageUpload/index.vue'

const loading = ref(false)
const showSearch = ref(true)
const total = ref(0)
const brandList = ref<ProductBrandVo[]>([])
const open = ref(false)
const title = ref('')

// 状态选项
const statusOptions = [
  { value: 1, label: '正常' },
  { value: 0, label: '禁用' }
]

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  name: undefined,
  status: undefined
})

// 表单参数
const form = ref<ProductBrandBo>({
  name: '',
  logo: '',
  description: '',
  sort: 0,
  status: 1
})

// 表单校验
const rules = {
  name: [
    { required: true, message: '品牌名称不能为空', trigger: 'blur' }
  ],
  sort: [
    { required: true, message: '排序不能为空', trigger: 'blur' }
  ],
  status: [
    { required: true, message: '状态不能为空', trigger: 'change' }
  ]
}

/** 查询品牌列表 */
const getList = async () => {
  try {
    loading.value = true
    const response = await listBrand(queryParams.value)
    brandList.value = response.rows
    total.value = response.total
  } catch (error) {
    console.error('获取品牌列表失败:', error)
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    status: undefined
  }
  handleQuery()
}

/** 新增按钮操作 */
const handleAdd = () => {
  form.value = {
    name: '',
    logo: '',
    description: '',
    sort: 0,
    status: 1
  }
  open.value = true
  title.value = '添加品牌'
}

/** 修改按钮操作 */
const handleUpdate = async (row: ProductBrandVo) => {
  try {
    const response = await getBrand(row.id)
    form.value = response.data
    open.value = true
    title.value = '修改品牌'
  } catch (error) {
    console.error('获取品牌信息失败:', error)
  }
}

/** 提交按钮 */
const submitForm = async () => {
  try {
    if (form.value.id) {
      await updateBrand(form.value)
      ElMessage.success('修改成功')
    } else {
      await addBrand(form.value)
      ElMessage.success('新增成功')
    }
    open.value = false
    getList()
  } catch (error) {
    console.error('提交失败:', error)
  }
}

/** 删除按钮操作 */
const handleDelete = async (row: ProductBrandVo) => {
  try {
    await ElMessageBox.confirm('是否确认删除该品牌？', '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await delBrand(row.id)
    ElMessage.success('删除成功')
    getList()
  } catch (error) {
    console.error('删除失败:', error)
  }
}

/** 取消按钮 */
const cancel = () => {
  open.value = false
  form.value = {
    name: '',
    logo: '',
    description: '',
    sort: 0,
    status: 1
  }
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  min-height: calc(100vh - 120px);
  background-color: var(--el-bg-color-page);
}

.box-card {
  border-radius: 8px;
  height: 100%;
  transition: all 0.3s;
}

.box-card:hover {
  box-shadow: var(--el-box-shadow-light);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 50px;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  font-size: 28px;
  color: var(--el-color-primary);
}

.header-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--el-text-color-primary);
}

.search-form {
  margin-bottom: 16px;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.brand-logo {
  width: 60px;
  height: 60px;
  border-radius: 4px;
  object-fit: contain;
}

.image-slot {
  width: 60px;
  height: 60px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--el-text-color-secondary);
  font-size: 12px;
  background-color: var(--el-fill-color-light);
  border-radius: 4px;
}

.image-slot .el-icon {
  font-size: 20px;
  margin-bottom: 4px;
}

:deep(.el-radio) {
  margin-right: 20px;
  height: 32px;
  line-height: 32px;
}

:deep(.el-radio__label) {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 100%;
}

:deep(.el-form-item) {
  margin-bottom: 24px;
}

:deep(.el-input-number) {
  width: 180px;
}

:deep(.el-dialog__header) {
  margin: 0;
  padding: 20px 32px;
  border-bottom: 1px solid var(--el-border-color-light);
}

:deep(.el-dialog__footer) {
  padding: 16px 32px;
  border-top: 1px solid var(--el-border-color-light);
}
</style> 